import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
const routes: RouteRecordRaw[] = [
  { path: "/", redirect: "/main" },
  { path: "/login", component: () => import("@views/login/LoginPage.vue") },
  {
    path: "/main",
    name: "主页",
    meta: { path: "/main" },
    component: () => import("@views/main/MainPage.vue"),
    children: [
      {
        path: "/main/publish",
        name: "发布帖子",
        meta: { path: "/main/publish" },
        component: () => import("@views/publish/PublishPage.vue"),
      },
      {
        path: "/main/course",
        name: "课程资源",
        meta: { path: "/main/course" },
        component: () => import("@views/course/CoursePage.vue"),
      },
      {
        path: "/main/personcenter",
        name: "个人中心",
        meta: { path: "/main/personcenter" },
        component: () => import("@views/person/PersonPage.vue"),
      },
      {
        path: "/main/coursedetail/:courseid(\\d+)",
        name: "课程详情",
        props: true,
        meta: { path: "/main/coursedetail" },
        component: () => import("@views/coursedetail/CourseDetailPage.vue"),
      },
      {
        path: "/main/postdetail/:postid(\\d+)",
        name: "帖子详情",
        props: true,
        meta: { path: "/main/postdetail" },
        component: () => import("@views/postdetail/PostDetailPage.vue"),
      },
    ],
  },
  {
    path: "/:path(.*)",
    component: () => import("@views/notfound/NotFound.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to) => {
  if (to.path !== "/login") {
    const token = localStorage.getItem("token");
    if (!token) {
      return "/login";
    }
  }
});

export default router;
